package com.example.administrator.foodrowfresh;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import com.example.administrator.foodrowfresh.fragmentframework.Classify;
import com.example.administrator.foodrowfresh.fragmentframework.Homepage;
import com.example.administrator.foodrowfresh.fragmentframework.Me;
import com.example.administrator.foodrowfresh.fragmentframework.Promotion;
import com.example.administrator.foodrowfresh.fragmentframework.ShoppingCart;
import com.example.administrator.foodrowfresh.superBase.MyBaseFragment;


public class ControlFragment extends AppCompatActivity {

    private MyBaseFragment mMe,mPromotion,mHomepage,mShopping,mClassify;
    private TextView mClassifyTV;
    private TextView mHomepageTV;
    private TextView mMeTV;
    private TextView mShoppingTV;
    private TextView mPromotionTV;
    private FragmentManager mfm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_control_fragment);
        initView();
    }
    public void initView()
    {
        mClassifyTV= (TextView) findViewById(R.id.classify_id);
        mHomepageTV= (TextView) findViewById(R.id.homepage_id);
        mMeTV= (TextView) findViewById(R.id.me_id);
        mPromotionTV= (TextView) findViewById(R.id.promotion_id);
        mShoppingTV= (TextView) findViewById(R.id.shopping_id);

        mfm=getSupportFragmentManager();
        //初始化选中
        mHomepageTV.performClick();
    }
    /*
    * 底部导航点击事件
    * */
    public void onClick(View view)
    {
        FragmentTransaction ft=mfm.beginTransaction();
        if (view!=null)
        {
            hideAllFragment(ft);
            switch (view.getId())
            {
                case R.id.homepage_id:
                    setSelect();
                    mHomepageTV.setSelected(true);
                    if (mHomepage==null)
                    {
                        mHomepage=new Homepage();
                        ft.add(R.id.fragment_control,mHomepage);
                    }else
                    {
                        ft.show(mHomepage);
                    }
                    break;
                case R.id.classify_id:
                    setSelect();
                    mClassifyTV.setSelected(true);
                    if (mClassify==null)
                    {
                        mClassify=new Classify();
                        ft.add(R.id.fragment_control,mClassify);
                    }else
                    {
                        ft.show(mClassify);
                    }
                    break;
                case R.id.promotion_id:
                    setSelect();
                    mPromotionTV.setSelected(true);
                    if (mPromotion==null)
                    {
                        mPromotion=new Promotion();
                        ft.add(R.id.fragment_control,mPromotion);
                    }else
                    {
                        ft.show(mPromotion);
                    }
                    break;
                case R.id.shopping_id:
                    setSelect();
                    mShoppingTV.setSelected(true);
                    if (mShopping==null)
                    {
                        mShopping=new ShoppingCart();
                        ft.add(R.id.fragment_control,mShopping);
                    }else
                    {
                        ft.show(mShopping);
                    }
                    break;
                case R.id.me_id:
                    setSelect();
                    mMeTV.setSelected(true);
                    if (mMe==null)
                    {
                        mMe=new Me();
                        ft.add(R.id.fragment_control,mMe);
                    }else
                    {
                        ft.show(mMe);
                    }
                    break;
                default:
                    break;
            }
            ft.commit();
        }
    }
    /*
    * 重置底部导航选项
    * */
    public void setSelect()
    {
        mClassifyTV.setSelected(false);
        mHomepageTV.setSelected(false);
        mMeTV.setSelected(false);
        mPromotionTV.setSelected(false);
        mShoppingTV.setSelected(false);
    }
    /*
    * 隐藏所有Fragment
    * */
    private void hideAllFragment(FragmentTransaction fragmentTransaction)
    {
        if(mMe != null)fragmentTransaction.hide(mMe);
        if(mClassify != null)fragmentTransaction.hide(mClassify);
        if(mHomepage != null)fragmentTransaction.hide(mHomepage);
        if(mPromotion != null)fragmentTransaction.hide(mPromotion);
        if(mShopping != null)fragmentTransaction.hide(mShopping);
    }

}
